package com.workstudy.wstudy.service.impl;

import java.util.List;

import com.workstudy.common.core.domain.entity.SysUser;
import com.workstudy.common.utils.DateUtils;
import com.workstudy.common.utils.ShiroUtils;
import com.workstudy.system.service.ISysConfigService;
import com.workstudy.system.service.ISysUserService;
import com.workstudy.wstudy.domain.UploadStudent;
import com.workstudy.wstudy.domain.WstApply;
import com.workstudy.wstudy.domain.WstPostUser;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.workstudy.wstudy.mapper.WstStudentMapper;
import com.workstudy.wstudy.domain.WstStudent;
import com.workstudy.wstudy.service.IWstStudentService;
import com.workstudy.common.core.text.Convert;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 学生Service业务层处理
 * 
 * @author sun
 * @date 2021-04-04
 */
@Service
public class WstStudentServiceImpl implements IWstStudentService 
{
    @Autowired
    private WstStudentMapper wstStudentMapper;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysConfigService configService;

    /**
     * 查询学生
     * 
     * @param studentId 学生ID
     * @return 学生
     */
    @Override
    public WstStudent selectWstStudentById(Long studentId)
    {
        return wstStudentMapper.selectWstStudentById(studentId);
    }

    /**
     * 查询学生列表
     * 
     * @param wstStudent 学生
     * @return 学生
     */
    @Override
    public List<WstStudent> selectWstStudentList(WstStudent wstStudent)
    {
        return wstStudentMapper.selectWstStudentList(wstStudent);
    }

    @Override
    public List<WstStudent> selectPostStuList(WstPostUser postUser,WstStudent wstStudent) {
        return wstStudentMapper.selectPostStuList(postUser,wstStudent);
    }

    @Override
    public List<WstPostUser> selectStuPostList(WstPostUser postUser) {
        return wstStudentMapper.selectStuPostList(postUser);
    }

    /**
     * 新增学生
     * 
     * @param wstStudent 学生
     * @return 结果
     */
    @Override
    @Transactional(isolation= Isolation.DEFAULT,propagation= Propagation.REQUIRED,rollbackFor = Exception.class)
    public int insertWstStudent(WstStudent wstStudent)
    {
        SysUser user = wstStudent;
        user.setSalt(ShiroUtils.randomSalt());
        user.setPassword(new Md5Hash(user.getLoginName() + user.getPassword() + user.getSalt()).toHex());
        user.setCreateBy(ShiroUtils.getLoginName());
        userService.insertUser(user);
        wstStudent.setStudentId(user.getUserId());
        wstStudent.setCreateTime(DateUtils.getNowDate());
        wstStudent.setCredit(30.0);
        return wstStudentMapper.insertWstStudent(wstStudent);
    }

    @Transactional(isolation= Isolation.DEFAULT,propagation= Propagation.REQUIRED,rollbackFor = Exception.class)
    public int insertWstStudentList(List<WstStudent> list)
    {
        int row = 0;
        for (WstStudent s:list){
            row+=wstStudentMapper.insertWstStudent(s);
        }
        return row;
    }

    /**
     * 修改学生
     * 
     * @param wstStudent 学生
     * @return 结果
     */
    @Override
    @Transactional(isolation= Isolation.DEFAULT,propagation= Propagation.REQUIRED,rollbackFor = Exception.class)
    public int updateWstStudent(WstStudent wstStudent)
    {
        SysUser user = wstStudent;
        userService.updateUser(user);
        wstStudent.setUpdateTime(DateUtils.getNowDate());
        return wstStudentMapper.updateWstStudent(wstStudent);
    }

    /**
     * 删除学生对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    @Transactional(isolation= Isolation.DEFAULT,propagation= Propagation.REQUIRED,rollbackFor = Exception.class)
    public int deleteWstStudentByIds(String ids)
    {
        userService.deleteUserByIds(ids);
        return wstStudentMapper.deleteWstStudentByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除学生信息
     * 
     * @param studentId 学生ID
     * @return 结果
     */
    @Override
    @Transactional(isolation= Isolation.DEFAULT,propagation= Propagation.REQUIRED,rollbackFor = Exception.class)
    public int deleteWstStudentById(Long studentId)
    {
        userService.deleteUserById(studentId);
        return wstStudentMapper.deleteWstStudentById(studentId);
    }

    @Override
    public int importWstStudent(List<UploadStudent> list) {
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        String password = configService.selectConfigByKey("sys.user.initPassword");
        int row =0;
        for (UploadStudent s:list){
            SysUser user = new SysUser();

        }
        return 0;
    }
}
